package br.edu.catolica.sadbc.core.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtil {

    /**
     * @param date
     * @return
     */
    static public String BrasilianDate2String(Date date) {
        DateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
        String result = simpleDateFormat.format(date);
        return result;
    }

    /**
     * @param string
     * @return
     */
    static public Date String2BrasilianDate(String string) {
    	Date result = null;
        if (string != null && !string.equals("") && !string.equals("31.12.9999")) {
           
            DateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
			try {
				result = simpleDateFormat.parse(string);
			} catch (ParseException e) {
				e.printStackTrace();
			}
            
        }
        return result;
    }
    
    static public Timestamp stringToTimestamp(String string){
    	DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
    	Date result = null;
    	try {
    		result = simpleDateFormat.parse(string);
    	} catch (ParseException e) {
    		e.printStackTrace();
    	}
    	Timestamp ts = new Timestamp(result.getTime());
    	return ts;
    }
    
   

    /**
	 * 
	 * @param dt
	 * @return
	 */
    static public Date formatDate(String dt) {

		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

		Date data = null;
		try {
			data = sdf.parse(dt);

		} catch (ParseException e) {

			e.printStackTrace();
		}

		return data;
	}
    
    /**
     * Recupera lista de Dates contendo os anos anteriores ao ano atual,
     * de acordo com o parametro passado.
     * 
     * @author Guilherme Broock - BRQ Curitiba
     * @param  numberYears
     * @return List<Date>
     */
    static public List<Date> getBeforeYears(int numberYears){
    	
    	List<Date> dates= new ArrayList<Date>();
    	GregorianCalendar gc = new GregorianCalendar();
		dates.add(gc.getTime());
		
		for(int i=0; i<numberYears; i++){
			gc.add(GregorianCalendar.YEAR, -1);
			dates.add(gc.getTime());
		}
    	return dates;
    }
    
    /**
     * Recupera o ano de um Date.
     * 
     * @author Guilherme Broock - BRQ Curitiba
     * @param  date
     * @return String
     */
    static public String getYear(Date date) {
	    SimpleDateFormat format = new SimpleDateFormat("yyyy");  
	    String strYear = format.format(date);
		return strYear;
    }
}
